package cn.minhx.mw.dtp.admin.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@EnableConfigurationProperties({RedisProperties.class})
public class RedisConfiguration {

    @Bean("dynamicThreadPoolRedissonClient")
    public RedissonClient redissonClient(RedisProperties redisProperties) {
        Config config = new Config().setCodec(JsonJacksonCodec.INSTANCE);
        config.useSingleServer()
                .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
                .setDatabase(redisProperties.getDatabase())
                .setPassword(redisProperties.getPassword());
        RedissonClient redissonClient = Redisson.create(config);
        log.info("[RedissonClient]初始化完成 {}:{} health={}", redisProperties.getHost(), redisProperties.getPort(), !redissonClient.isShutdown());
        return redissonClient;
    }

}
